﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Data;

using ActiproSoftware.Windows.Controls.Docking;
using ActiproSoftware.Windows.Controls.Docking.Serialization;
using ActiproSoftware.Windows.Controls.Docking.Switching;

using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Presentation.Regions;

using SupremacyEditor.Services;

namespace SupremacyEditor
{
    /// <summary>
    /// Interaction logic for EditorControl.xaml
    /// </summary>
    public partial class EditorControl
    {
        protected readonly DelegateCommand<object> LoadLayoutCommand;
        protected readonly DelegateCommand<object> SaveLayoutCommand;
        protected readonly DelegateCommand<string> ActivateToolWindowCommand;

        public EditorControl(/*ITextService textService*/)
        {
            /*if (textService == null)
                throw new ArgumentNullException("textService");

            StringResourceProvider.SetProvider(this, new StringResourceProvider(textService));*/

            InitializeComponent();

            LoadLayoutCommand = new DelegateCommand<object>(
                delegate
                {
                    try
                    {
                        //if (File.Exists(".layout"))
                        //    new DockSiteLayoutSerializer().LoadFromFile(".layout", this.DockSite);
                    }
                    catch {}
                },
                o => true) { IsActive = true };

            SaveLayoutCommand = new DelegateCommand<object>(
                delegate
                {
                    try
                    {
                        new DockSiteLayoutSerializer().SaveToFile(".layout", this.DockSite);
                    }
                    catch {}
                },
                o => true) { IsActive = true };

            ActivateToolWindowCommand = new DelegateCommand<string>(
                delegate(string windowName)
                {
                    var window = this.DockSite.GetDockingWindow(windowName);
                    if (window == null)
                        return;
                    if (!window.IsOpen)
                        window.Open();
                    window.Activate(true);
                }) { IsActive = true };

            EditorCommands.SaveLayout.RegisterCommand(SaveLayoutCommand);
            EditorCommands.LoadLayout.RegisterCommand(LoadLayoutCommand);
            EditorCommands.ActivateToolWindow.RegisterCommand(ActivateToolWindowCommand);

            this.DockSite.Loaded += OnLoaded;
            this.DockSite.WindowClosed += OnDockSiteWindowClosed;
            this.TabHost.SetBinding(
                RegionManager.RegionManagerProperty,
                new Binding
                {
                    Source = this,
                    Path = new PropertyPath(RegionManager.RegionManagerProperty),
                    Mode = BindingMode.OneWay
                });

            EditorEvents.EditorClosing.Subscribe(
                delegate
                {
                    if (this.DockSite.IsLoaded && EditorCommands.SaveLayout.CanExecute(null))
                        EditorCommands.SaveLayout.Execute(null);
                });
        }

        private void OnDockSiteWindowClosed(object sender, DockingWindowEventArgs e)
        {
            if (this.TabHost.Content == null)
                this.TabHost.Content = this.TabContainer;

            var item = e.Window.DataContext as IEditorItem;
            if (item == null)
                return;

            EditorEvents.ItemClosed.Publish(item);
        }

        private static void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (EditorCommands.LoadLayout.CanExecute(null))
                EditorCommands.LoadLayout.Execute(null);
            EditorEvents.EditorLoaded.Publish(EventArgs.Empty);
        }
    }
}
